package xcmg.device.web.purchase;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.exception.CommonBusiExceptions;
import xcmg.device.service.basic.MaterialsService;
import xcmg.device.service.purchase.PurchaseInboundService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import xcmg.device.service.vo.purchase.PurchaseInboundAddVO;
import xcmg.device.service.vo.purchase.PurchaseInboundDetailVO;
import xcmg.device.service.vo.purchase.PurchaseInboundVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/purchaseInbound")
public class PurchaseInboundController extends BasicController {
    @Autowired
    private PurchaseInboundService purchaseInboundService;

    @Autowired
    private MaterialsService materialsService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation("查询采购入库记录列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<PurchaseInboundVO>> list(@RequestBody SearchCommonVO<PurchaseInboundVO> condition) throws Exception {
        PurchaseInboundVO purchaseInboundVO = condition.getFilters();
        if (purchaseInboundVO == null) {
            purchaseInboundVO = new PurchaseInboundVO();
        }
        purchaseInboundVO.setOrgId(getOrgId());
        condition.setFilters(purchaseInboundVO);
        PageCommonVO<PurchaseInboundVO> pageCommonVO = purchaseInboundService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "增加采购入库记录")
    @FastMappingInfo(needLogin = true, code = AuthCode.StockIn.PURCHASE_IN_ADD)
    public ActionResult<String> warehouse(@RequestBody PurchaseInboundAddVO purchaseInboundAddVO) throws Exception {
        if (null == purchaseInboundAddVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode errorCode = purchaseInboundService.batchAdd(purchaseInboundAddVO, getUserId(), getOrgId());
            return actionResult(errorCode);
        } catch (Exception e) {
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新采购入库记录")
    @FastMappingInfo(needLogin = true, code = AuthCode.StockIn.PURCHASE_IN_EDIT)
    public ActionResult<String> update(@RequestBody PurchaseInboundAddVO purchaseInboundAddVO) throws Exception {
        if (null == purchaseInboundAddVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode errorCode = purchaseInboundService.update(purchaseInboundAddVO, getOrgId());
            return actionResult(errorCode);
        } catch (Exception e) {
            return new ActionResult<>(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ApiOperation("采购入库记录删除")
    @FastMappingInfo(needLogin = true, code = AuthCode.StockIn.PURCHASE_IN_DEL)
    public ActionResult<String> remove(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(purchaseInboundService.remove(Arrays.asList(ids), getOrgId()));
    }

    @RequestMapping(value = "/item", method = RequestMethod.GET)
    @ApiOperation(value = "查找单条记录详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PurchaseInboundAddVO> item(String id) throws Exception {
        return actionValueResult(purchaseInboundService.item(id, getOrgId()));
    }

    @RequestMapping(value = "/download", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        materialsService.downloadPurchaseInbound();
    }

    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiOperation("零件批量导入")
    @FastMappingInfo(needLogin = true)
    public ActionResult<List<PurchaseInboundDetailVO>> importExcel(HttpServletRequest request ) throws Exception{
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        String supplierOrgId = request.getParameter("supplierOrgId");
        if (supplierOrgId == null || supplierOrgId.trim().isEmpty()) {
            throw new CommonBusiExceptions("入参不正确，请先选择供货商");
        }
        FunctionResult<List<PurchaseInboundDetailVO>> result = new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            throw new CommonBusiExceptions(ErrorCode.ImportExcelEndWithXLS.getDesc());
        } else {
            InputStream in = file.getInputStream();
            result = purchaseInboundService.importExcel(in, supplierOrgId, getOrgId());
        }
        return actionResult(result.getCode(), result.getValue());
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation("导出采购入库记录列表")
    @FastMappingInfo(needLogin = true, code = AuthCode.StockIn.PURCHASE_IN_EXPORT)
    public void export(String[] ids) throws Exception {
        purchaseInboundService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll", method = RequestMethod.POST)
    @ApiOperation("导出全部采购入库记录列表")
    @FastMappingInfo(needLogin = true, code = AuthCode.StockIn.PURCHASE_IN_EXPORT)
    public void exportAll(PurchaseInboundVO condition) throws Exception {
        condition.setOrgId(getOrgId());
        purchaseInboundService.exportAll(condition);
    }

    @RequestMapping(value = "/manualFinish", method = RequestMethod.POST)
    @ApiOperation("手动完成发货单")
    public ActionResult<String> manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception {
        return actionResult(purchaseInboundService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
    }


    @RequestMapping(value = "/receiptConfirm", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> receiptConfirm(@RequestBody PurchaseInboundVO condition) {
        return actionResult(purchaseInboundService.receiptConfirm(condition.getId(), getUserName()));
    }
}
